package com.cheil.publish.service.ai;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import org.springframework.ai.image.ImageOptions;

/**
 * DashscopeImageOptions代表使用Dashscope图像模型生成图像的选项。
 *
 * <p>
 * 它实现了ImageOptions接口，可以在不同的图像生成模型之间移植。
 * </p>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DashscopeImageOptions implements ImageOptions {

    /**
     * 要使用的模型进行图像生成。
     * 默认值: "qwen-image"
     */
    @JsonProperty("model")
    private String model = "qwen-image";

    /**
     * 生成的图像数量。
     * 默认值: 1
     */
    @JsonProperty("n")
    private Integer n = 1;

    /**
     * 生成图像的尺寸。
     * 格式: "宽*高"，例如 "1024*1024"
     * 默认值: "1024*1024"
     */
    @JsonProperty("size")
    private String size = "928*1664";

    /**
     * 反向提示词，用来描述不希望在画面中看到的内容
     */
    @JsonProperty("negative_prompt")
    private String negativePrompt;

    /**
     * 是否开启prompt智能改写
     */
    @JsonProperty("prompt_extend")
    private Boolean promptExtend;

    /**
     * 是否添加水印标识
     */
    @JsonProperty("watermark")
    private Boolean watermark;

    /**
     * 随机种子
     */
    @JsonProperty("seed")
    private String seed;

    public static Builder builder() {
        return new Builder();
    }

    @Override
    @JsonIgnore
    public Integer getN() {
        return this.n;
    }

    @Override
    public String getModel() {
        return this.model;
    }

    @Override
    @JsonIgnore
    public Integer getWidth() {
        if (size != null && size.contains("*")) {
            try {
                return Integer.parseInt(size.split("\\*")[0]);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    @Override
    @JsonIgnore
    public Integer getHeight() {
        if (size != null && size.contains("*")) {
            try {
                return Integer.parseInt(size.split("\\*")[1]);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    @Override
    @JsonIgnore
    public String getResponseFormat() {
        return null;
    }

    @Override
    @JsonIgnore
    public String getStyle() {
        return null;
    }

    public String getSize() {
        return this.size;
    }

    public String getNegativePrompt() {
        return this.negativePrompt;
    }

    public Boolean getPromptExtend() {
        return this.promptExtend;
    }

    public Boolean getWatermark() {
        return this.watermark;
    }

    public String getSeed() {
        return this.seed;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof DashscopeImageOptions)) {
            return false;
        }
        DashscopeImageOptions that = (DashscopeImageOptions) o;
        return Objects.equals(this.model, that.model) &&
                Objects.equals(this.n, that.n) &&
                Objects.equals(this.size, that.size) &&
                Objects.equals(this.negativePrompt, that.negativePrompt) &&
                Objects.equals(this.promptExtend, that.promptExtend) &&
                Objects.equals(this.watermark, that.watermark) &&
                Objects.equals(this.seed, that.seed);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.model, this.n, this.size, this.negativePrompt, this.promptExtend, this.watermark, this.seed);
    }

    @Override
    public String toString() {
        return "DashscopeImageOptions{" +
                "model='" + this.model + '\'' +
                ", n=" + this.n +
                ", size='" + this.size + '\'' +
                ", negativePrompt='" + this.negativePrompt + '\'' +
                ", promptExtend=" + this.promptExtend +
                ", watermark=" + this.watermark +
                ", seed='" + this.seed + '\'' +
                '}';
    }

    public static class Builder {
        private final DashscopeImageOptions options = new DashscopeImageOptions();

        public Builder model(String model) {
            this.options.model = model;
            return this;
        }

        public Builder n(Integer n) {
            this.options.n = n;
            return this;
        }

        public Builder size(String size) {
            this.options.size = size;
            return this;
        }

        public Builder widthHeight(Integer width, Integer height) {
            this.options.size = width + "*" + height;
            return this;
        }

        public Builder negativePrompt(String negativePrompt) {
            this.options.negativePrompt = negativePrompt;
            return this;
        }

        public Builder promptExtend(Boolean promptExtend) {
            this.options.promptExtend = promptExtend;
            return this;
        }

        public Builder watermark(Boolean watermark) {
            this.options.watermark = watermark;
            return this;
        }

        public Builder seed(String seed) {
            this.options.seed = seed;
            return this;
        }

        public DashscopeImageOptions build() {
            return this.options;
        }
    }
}